/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.disco.gco.view;

import com.jjsoft.disco.gco.mdl.Proveedor;
import com.jjsoft.disco.gco.ngc.ProveedorManager;
import com.jjsoft.disco.gco.util.MiModelo;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Johnny
 */
public class VMantenimientoProveedores extends javax.swing.JFrame {

    /**
     * Creates new form VMantenimientoProveedores
     */
    ProveedorManager pm = new ProveedorManager();
    List<Proveedor> listaProveedores = new ArrayList<Proveedor>();
    private static VMantenimientoProveedores instancia;
    public VMantenimientoProveedores() {
        initComponents();
        listaProveedores = pm.listarProveedores();
        cargarDatos();
    }
    public static VMantenimientoProveedores getInstance()
    {
        if(instancia==null)
            instancia=new VMantenimientoProveedores();
        return instancia;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        cmdTodo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtgProveedores = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtMatch = new javax.swing.JTextField();
        cmdBuscar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        cmdModificar = new javax.swing.JButton();
        cmdNuevo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
        jLabel2.setText("Mantenimiento de Proveedores");

        cmdTodo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdTodo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/listar.png"))); // NOI18N
        cmdTodo.setText("    Mostrar Todos");
        cmdTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTodoActionPerformed(evt);
            }
        });

        dtgProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        dtgProveedores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dtgProveedores);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setText("Buscar en la tabla:");

        txtMatch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMatchActionPerformed(evt);
            }
        });
        txtMatch.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtMatchPropertyChange(evt);
            }
        });
        txtMatch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMatchKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMatchKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtMatchKeyTyped(evt);
            }
        });

        cmdBuscar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        cmdBuscar.setText("Buscar");
        cmdBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdBuscarActionPerformed(evt);
            }
        });

        cmdEliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/delete.png"))); // NOI18N
        cmdEliminar.setText("Eliminar");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        cmdModificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit.png"))); // NOI18N
        cmdModificar.setText("Modificar");
        cmdModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdModificarActionPerformed(evt);
            }
        });

        cmdNuevo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/new.png"))); // NOI18N
        cmdNuevo.setText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 861, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cmdBuscar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cmdEliminar)
                            .addGap(18, 18, 18)
                            .addComponent(cmdModificar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmdNuevo))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(cmdTodo)
                            .addGap(676, 676, 676)))
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 339, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addGap(7, 7, 7)
                    .addComponent(cmdTodo)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmdModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtMatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmdBuscar)
                            .addComponent(cmdNuevo)
                            .addComponent(cmdEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        setSize(new java.awt.Dimension(877, 378));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTodoActionPerformed
        // TODO add your handling code here:
        listaProveedores = pm.listarProveedores();
        cargarDatos();
    }//GEN-LAST:event_cmdTodoActionPerformed

    private void txtMatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMatchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchActionPerformed

    private void txtMatchPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtMatchPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchPropertyChange

    private void txtMatchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyPressed
        // TODO add your handling code here:
        if(evt.getKeyCode()==10)
        {
            cmdBuscar.doClick();
        }
    }//GEN-LAST:event_txtMatchKeyPressed

    private void txtMatchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyReleased

    private void txtMatchKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMatchKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMatchKeyTyped

    private void cmdBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBuscarActionPerformed
        // TODO add your handling code here:
        if (txtMatch.getText().length() > 0) {
            listaProveedores = pm.listarProveedores(txtMatch.getText());
            cargarDatos();
        }
    }//GEN-LAST:event_cmdBuscarActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed
        // TODO add your handling code here:
        if (dtgProveedores.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(null, "Primero seleccione un Proveedor", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int i = dtgProveedores.getSelectedRow();
            if(JOptionPane.showConfirmDialog(rootPane, "Esta seguro de eliminar este proveedor?","Eliminar",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
            {
                pm.eliminarProveedor(listaProveedores.get(i));
                listaProveedores.remove(i);
                cargarDatos();
            }
        }
    }//GEN-LAST:event_cmdEliminarActionPerformed

    private void cmdModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdModificarActionPerformed
        // TODO add your handling code here:
        if (dtgProveedores.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(null, "Primero seleccione un Producto", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            new VNuevoProveedor(listaProveedores.get(dtgProveedores.getSelectedRow()),cmdTodo).show();
            
        }
    }//GEN-LAST:event_cmdModificarActionPerformed

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        // TODO add your handling code here:
        new VNuevoProveedor(cmdTodo).setVisible(true);
    }//GEN-LAST:event_cmdNuevoActionPerformed
     private void cargarDatos() {
        MiModelo modelo = new MiModelo();
        String[] identificadores = new String[]{"Id", "Nombre", "Ciudad", "Direccion", "Telefono", "EMail"};
        modelo.setColumnIdentifiers(identificadores);
        for (int i = 0; i < listaProveedores.size(); i++) {
                String[] proveedor = new String[]{listaProveedores.get(i).getIdProveedor().toString(), listaProveedores.get(i).getNombre(), listaProveedores.get(i).getCiudad(), listaProveedores.get(i).getDireccion(), listaProveedores.get(i).getTelefono(), listaProveedores.get(i).getEmail()};
                modelo.addRow(proveedor);
            
        }
        dtgProveedores.setModel(modelo);
        dtgProveedores.getColumn("Id").setPreferredWidth(25);
        dtgProveedores.getColumn("Telefono").setPreferredWidth(45);
        dtgProveedores.getColumn("EMail").setPreferredWidth(150);
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoProveedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoProveedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoProveedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VMantenimientoProveedores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VMantenimientoProveedores().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdBuscar;
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdModificar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdTodo;
    private javax.swing.JTable dtgProveedores;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtMatch;
    // End of variables declaration//GEN-END:variables
}
